<?php
/**
 * A simple description for this script
 *
 * PHP Version 5.2.0 or Upper version
 *
 * @package    Paylog
 * @author     Hidehito NOZAWA aka Suin <http://suin.asia>
 * @copyright  2010 Hidehito NOZAWA
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 */

class Paylog_Controller_Detail extends Paylog_Abstract_PageController
{
	protected $balanceHand = null;
	protected $bookId = null;
	protected $start  = null;
	protected $end    = null;

	public function __construct()
	{
		parent::__construct();
		$this->balanceHand = new Paylog_Object_BalanceHandler;

		$this->start  = date('Y-m-d');
		$this->end    = date('Y-m-t');

		$this->start  = Paylog::get('start', $this->start);
		$this->end    = Paylog::get('end', $this->end);
		$this->bookId = Paylog::get('book_id');

		$this->output['start']   =& $this->start;
		$this->output['end']     =& $this->end;
		$this->output['book_id'] =& $this->bookId;

		$this->menu['start']   =& $this->start;
		$this->menu['end']     =& $this->end;
		$this->menu['book_id'] =& $this->bookId;
		$this->menu['month']   = preg_replace('/-[0-9]{2}$/', '', $this->start);
	}

	public function main()
	{
		$this->_validateAccess();
		$this->_default();
	}

	protected function _default()
	{
		$startDay = strtotime($this->start);
		$endDay   = strtotime($this->end);

		$balanceObjs = $this->balanceHand->loadMonthly($startDay, $endDay, $this->bookId);

		$items = array();

		$outgoTotal  = 0;
		$incomeTotal = 0;

		foreach ( $balanceObjs as $balanceObj )
		{
			$tags = array();

			foreach ( $balanceObj->tagObjs as $tagObj )
			{
				$groupId = $tagObj->getVar('group_id');
				$tags[$groupId] = array(
					'id'       => $tagObj->getVar('id'),
					'name'     => $tagObj->getVar('name'),
					'group_id' => $groupId,
				);
			}

			$flow = $balanceObj->getVar('flow');

			if ( $flow == Paylog_Object_Balance::FLOW_OUT )
			{
				$outgo  = $balanceObj->getVar('price');
				$income = 0;
				$outgoTotal += $outgo;
			}
			else
			{
				$income = $balanceObj->getVar('price');
				$outgo  = 0;
				$incomeTotal += $income;
			}

			$items[] = array(
				'id'     => $balanceObj->getVar('id'),
				'name'   => $balanceObj->getVar('name'),
				'date'   => $balanceObj->getVar('date'),
				'income' => number_format($income),
				'outgo'  => number_format($outgo),
				'tags'   => $tags,
			);
		}

		$this->output['items'] = $items;

		$groupHand = new Paylog_Object_GroupHandler;
		$groupObjs = $groupHand->loads();

		foreach ( $groupObjs as $groupObj )
		{
			$this->output['groups'][] = array(
				'id'   => $groupObj->getVar('id'),
				'name' => $groupObj->getVar('name'),
			);
		}

		$this->output['date_format'] = Paylog::msg("%Y-%m-%d");
		$this->output['outgo_total' ] = number_format($outgoTotal);
		$this->output['income_total'] = number_format($incomeTotal);

		$this->output['current'] = array(
			'year'  => date('Y', $startDay),
			'month' => date('m', $startDay),
		);

		$this->_view();
	}

	protected function _validateAccess()
	{
		if ( !$this->bookId )
		{
			Paylog::location();
		}

		if ( !$this->_validateDate($this->start) )
		{
			Paylog::redirect("Start date is invalid.");
		}

		if ( !$this->_validateDate($this->end) )
		{
			Paylog::redirect("End date is invalid.");
		}

		$bookHand = new Paylog_Object_BookHandler;

		if ( !$bookHand->isBookExists($this->bookId) )
		{
			Paylog::redirect("Book deos not exists.");
		}
	}
}

?>
